package com.zl.smartmall.library.listener;


import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

public abstract class CheckPhoneNumber extends BasePhoneListener {
	
	protected int mReqType = -1;
	public static final int CHECK_PHONE_NUMBER = 1;
	public static final int GET_RESETPASSWORD_SMSCODE = 2;
	public static final int CHANGE_PASSWORD = 3;
	
	public void setmReqType(int mReqType) {
		this.mReqType = mReqType;
	}

	@Override
	public void onNoMoreData() {

	}

	@Override
	public void onOffline() {
	}

	public abstract void onResponseSuccess(String phone,String smsCode);
	
	public abstract void onPhoneError();
	
	public abstract void onPhoneIsUsed();
	
	@Override
	public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
		try {
			int ret;
			switch (mReqType) {
			case CHECK_PHONE_NUMBER:
//				ret = response.getInt("ret");
//				switch (ret) {
//				case 0:
//					JSONObject result  = response.getJSONObject("result");
//					String phone = result.getString("phone");
//					String code = result.getString("smscode");
//					onResponseSuccess(phone, code);
//					break;
//				case 1:
//					onPhoneError();
//					break;
//				case 3:
//					onPhoneIsUsed();
//					break;
//
//				default:
//					onResponseFailed();
//					break;
//				}
//				break;
			case GET_RESETPASSWORD_SMSCODE:
				ret = response.getInt("ret");
				switch (ret) {
				case 0:
					JSONObject result  = response.getJSONObject("result");
					String phone = result.getString("phone");
//					String code = result.getString("smscode");
					onResponseSuccess(phone, "");
					break;
				case 1:
					onPhoneError();
					break;
				case 3:
					onPhoneIsUsed();
					break;
				case 6:
					passthree();
					break;
				case 4:
					passtwo();
					break;

				default:
					onResponseFailed();
					break;
				}
				break;
				
			case CHANGE_PASSWORD:
				ret = response.getInt("ret");
				switch (ret) {
				case 0:
					onResponseSuccess("", "");
					break;
				case 3:
					onPhoneError();
					break;
				case 6:
					passthree();
					break;
				case 4:
					passtwo();
					break;
				default:
					onResponseFailed();
					break;
				}
				break;

			default:
				break;
			}
			
		} catch (JSONException e) {
			e.printStackTrace();
			onResponseFailed();
		}
	}
	
	@Override
	public void onFailure(int statusCode, Header[] headers,
			String responseString, Throwable throwable) {
		super.onFailure(statusCode, headers, responseString, throwable);
		onResponseFailed();
	}

}
